import http from '../utils/http';

/**
 * 获取列表
 */
function login(loginName,password){
    return new Promise((resolve, reject) => {
        let param={loginName:loginName,password:password}
        http("get",'/user/login',param).then(res => {
            console.log("/user/login-------:"+res);
            resolve (res);
            return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}

function logout(){
    return new Promise((resolve, reject) => {
        http("get",'/user/logout').then(res => {
            resolve (res);
            return;
        },error => {
            console.log("logout error!~",error);
            reject(error)
        })
    })
}



/**
 * 获取功能菜单数据
 */
function getMenus(){
    return new Promise((resolve, reject) => {
        http("get",'/user/getMemus').then(res => {
            //console.log("getMenus-------:"+res);
            resolve (res);
        },error => {
            console.log("getMenus error!~",error);
            reject(error)
        })
    })
}


/**
 * 获取列表
 */
function getUserList(){
    return new Promise((resolve, reject) => {
        http("get",'/user/userList').then(res => {
            resolve (res);
        },error => {
            console.log("userList网络异常~",error);
            reject(error)
        })
    })
}

/**
 * 获取列表
 */
function getRoleList(){
    return new Promise((resolve, reject) => {
        http("get",'/user/roleList').then(res => {
            resolve (res);
        },error => {
            console.log("role List~",error);
            reject(error)
        })
    })
}


/**
 * 获取列表
 */
function hasLoginName(loginName){
    return new Promise((resolve, reject) => {
        let param={loginName:loginName}
        http("get",'/user/hasLoginName',param).then(res => {
            console.log("/user/login-------:"+res);
            resolve (res);
            return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}

/**
 * 获取列表
 */
function saveUser(loginName,name,userFlag,userId){
    return new Promise((resolve, reject) => {
        let param={loginName:loginName,name:name,userFlag:userFlag,id:userId}
        http("get",'/user/saveUser',param).then(res => {
            resolve (res);
            return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}


/**
 * 获取列表
 */
function delUser(userId){
    return new Promise((resolve, reject) => {
        let param={id:userId}
        http("get",'/user/delUser',param).then(res => {
            resolve (res);
            return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}
/**
 * get user
 */
function getUser(userId){
    return new Promise((resolve, reject) => {
        let param={id:userId}
        http("get",'/user/getUser',param).then(res => {
            resolve (res);
            return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}


/**
 * get user
 */
function getResources(){
    return new Promise((resolve, reject) => {
        http("get",'/user/getResources').then(res => {
            resolve (res);
            return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}

// save Resource
function saveResource(param){
    return new Promise((resolve, reject) => {
        http("post",'/user/saveResource',param).then(res => {
            resolve (res);
            return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}

// save Resource
function removeResourceById(id){
    return new Promise((resolve, reject) => {
        http("get",'/user/removeResourceById',{id:id}).then(res => {
            resolve (res);
            //return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}


// save Resource
function removeRole(id){
    return new Promise((resolve, reject) => {
        http("get",'/user/delRole',{id:id}).then(res => {
            resolve (res);
            return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}

// query
function getFunsByRoleId(roleId) {
    return new Promise((resolve, reject) => {
        http("get",'/user/getFunsByRoleId',{roleId:roleId}).then(res => {
            resolve (res);
            return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}


// query
function getRolesByUserId(userId) {
    return new Promise((resolve, reject) => {
        http("get",'/user/getRolesByUserId',{userId:userId}).then(res => {
            resolve (res);
            return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}



// saveRelations
function saveRelations(roleId,functionIds) {
    return new Promise((resolve, reject) => {
        http("post",'/user/saveFunsByRoleId',{roleId:roleId,functionIds:functionIds}).then(res => {
            resolve (res);
            //return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}

// saveUserToRole
function saveUserToRole(userId,roleIds) {
    return new Promise((resolve, reject) => {
        http("post",'/user/saveUserToRole',{userId:userId,roleIds:roleIds}).then(res => {
            resolve (res);
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}


function saveRole(name) {
    return new Promise((resolve, reject) => {
        http("get",'/user/saveRole',name).then(res => {
            resolve (res);
            //return;
        },error => {
            console.log("login error!~",error);
            reject(error)
        })
    })
}


export {
    login,logout,getMenus,getUserList,getRoleList,
    hasLoginName,saveUser,delUser,getUser,getResources,
    saveResource,removeResourceById,getFunsByRoleId,saveRelations,saveRole,
    removeRole,getRolesByUserId,saveUserToRole
}

